import { pbRequest, loading } from "@/applications/sys/public/pbRequest/index"
import PublicDataSource from '@/applications/sys/utils/js/table-handle-class/publicDataSource'
import { DataTool } from '@/js/tool-class/dataTool'
class DataSource extends PublicDataSource {
  constructor({
    modules = '',
    selectUri = '/',
    deleteUri = '/',
    selectOneUri = '/',
    pageSize = 20,
    tableHeader
  } = {}) {
    super({
      modules,
      selectUri,
      deleteUri,
      selectOneUri,
      pageSize,
      tableHeader
    })
  }

  isTimestamp(timestamp) {
    let regex = /^\d{10}$/
    if (regex.test(timestamp)) {
      let date = new Date(timestamp * 1000)
      return !isNaN(date.getTime())
    }
    return false
  }

  // forMatData(key, value, rowData, column) {
  //   switch (key) {
  //   default: {
  //     if (new DataTool().isTimestamp(value))
  //       return new DataTool().timeStamp2Time(+value, 'yyyy-MM-dd hh:mm:ss')
  //     else return value && value.toString().length > 0 ? value : typeof value === 'number' ? value : '-'
  //   }
  //   }
  // }

  async getHeader(context = this, tableRef){
    let { data: { data, code, message } } = await pbRequest.post(`${context.selectUri}?currentPage=${1}&pageSize=${1}`, context.searchData)
    if (code !== 200 || !data) return

  }

  async initData(context = this, tableRef) {
    let { data: { data, code, message } } = await pbRequest.post(`${context.selectUri}?currentPage=${context.currentPage}&pageSize=${context.pageSize}`, context.searchData)
    if (code !== 200 || !data) return
    context.total = data.resultDataList.total

    if (context.tableHeader.length === 0){
      context.tableHeader.push(... [
        {
          title: "批次号",
          dataKey: "batchCode",
          key: 'batchCode',
          width: 200,
          type: 'text',
          isShow: true,
          isFixed: false
        }, {
          title: "状态",
          dataKey: "status",
          key: 'status',
          width: 200,
          type: 'text',
          isShow: true,
          isFixed: false
        }, {
          title: "所属经销商",
          dataKey: "belongArchiveName",
          key: 'belongArchiveName',
          width: 200,
          type: 'text',
          isShow: true,
          isFixed: false
        }, {
          title: "账单编码",
          dataKey: "billCode",
          key: 'billCode',
          width: 200,
          type: 'text',
          isShow: true,
          isFixed: false
        }, {
          title: "账单类型",
          dataKey: "billType",
          key: 'billType',
          width: 200,
          type: 'text',
          isShow: true,
          isFixed: false
        }, {
          title: "账期",
          dataKey: "costTerm",
          key: 'costTerm',
          width: 200,
          type: 'text',
          isShow: true,
          isFixed: false
        }
      ])
      tableRef?.handleRender()
    }
    let tempHeader = data.resultNameList.map(item => {return {
      title: item.resultName,
      dataKey: item.resultCode,
      key: item.resultCode,
      dataType: item.resultType,
      width: 200,
      type: 'text',
      isShow: true,
      isFixed: false
    }})
    context.tableHeader.push(...tempHeader)

    context.tableData = data.resultDataList.list.map(item => {
      Object.keys(item.keyMap).forEach(key => {
        if (new DataTool().isTimestamp(item.keyMap[key])){
          item.keyMap[key] = new DataTool().timeStamp2Time(+item.keyMap[key], 'yyyy-MM-dd hh:mm:ss')
        }
      })
      return item.keyMap
    })
  }
  currentPageChange(page, context = this, tableRef) {
    context.currentPage = page
    context.initData(context, tableRef)
  }
  pageSizeChange(pageSize, context = this, tableRef) {
    context.currentPage = 1
    context.pageSize = pageSize
    context.initData(context, tableRef)
  }
  search(searchData, context = this, tableRef) {
    context.searchData = searchData
    context.currentPage = 1
    context.initData(context, tableRef, true)
  }

  async updateBill(params) {
    let { data: { code, data, msg } } = await pbRequest.post(`/colorful-fog/CollectResultMain/manualUpdate`, params)
    return code === 200 ? { data, code } : msg
  }

  async checkUpdateData(params) {
    let { data: { code, data, msg } } = await pbRequest.post(`/colorful-fog/UpdateBillLog/selectOneFieldLog`, params)
    return code === 200 ? { data, code } : msg
  }

  async getCollectExportData(params) {
    let { data: { code, data, msg } } = await pbRequest.post(`/colorful-fog/CollectResultMain/exportExcel`, params)
    return code === 200 ? { data, code } : msg
  }

  async updateBill(params) {
    let { data: { code, data, msg } } = await pbRequest.post(`/colorful-fog/schemeMain/flash`, params)
    return code === 200 ? { data, code } : msg
  }

}

export { loading, DataSource }